package com.itheima.service.db.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.domain.db.AnalysisByDay;
import com.itheima.mapper.AnalysisByDayMapper;
import com.itheima.mapper.LogMapper;
import com.itheima.service.db.AnalysisByDayService;
import com.itheima.util.ComputeUtil;
import com.itheima.vo.AnalysisSummaryVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

@DubboService
public class AnalysisByDayServiceImpl implements AnalysisByDayService {

    @Autowired
    AnalysisByDayMapper analysisByDayMapper;

    @Autowired
    LogMapper logMapper;

    @Override
    public void saveOrUpdate() {
        //准备今天的字符串
        String today = DateUtil.offsetDay(new Date(), 0).toDateStr();
        //准备昨天的字符串
        String yesterday = DateUtil.offsetDay(new Date(), -1).toDateStr();

        //判断今天有无记录
        LambdaQueryWrapper<AnalysisByDay> qw = new LambdaQueryWrapper<>();
        qw.eq(AnalysisByDay::getRecordDate,today);
        AnalysisByDay analysisByDay = analysisByDayMapper.selectOne(qw);

        if (analysisByDay != null) {
            //若有就更新
            analysisByDay.setNumRegistered(logMapper.findRegisterOrLoginCount(today,"0102"));
            analysisByDay.setNumActive(logMapper.findActiveCount(today));
            analysisByDay.setNumLogin(logMapper.findRegisterOrLoginCount(today,"0101"));
            analysisByDay.setNumRetention1d(logMapper.findRetention1dCount(today,yesterday));

            analysisByDayMapper.updateById(analysisByDay);
        }else{
            //若无就保存
            analysisByDay = new AnalysisByDay();

            analysisByDay.setRecordDate(new Date());
            analysisByDay.setNumRegistered(logMapper.findRegisterOrLoginCount(today,"0102"));
            analysisByDay.setNumActive(logMapper.findActiveCount(today));
            analysisByDay.setNumLogin(logMapper.findRegisterOrLoginCount(today,"0101"));
            analysisByDay.setNumRetention1d(logMapper.findRetention1dCount(today,yesterday));

            analysisByDayMapper.insert(analysisByDay);
        }

    }

    @Override
    public AnalysisSummaryVo findSummary() {
        //1.准备数据
        //准备今天字符串,昨天字符串,7天字符串,30天字符串
        String today = DateUtil.offsetDay(new Date(), 0).toDateStr();
        String yesterDay = DateUtil.offsetDay(new Date(), -1).toDateStr();
        String passWeekDay = DateUtil.offsetDay(new Date(), -7).toDateStr();
        String passMonthDay = DateUtil.offsetDay(new Date(), -30).toDateStr();

        //准备今天数据
        AnalysisByDay todayAnalysis = analysisByDayMapper.findByRecordDate(today);
        //准备昨天数据
        AnalysisByDay yesterdayAnalysis = analysisByDayMapper.findByRecordDate(yesterDay);


        //2.创建vo对象
        AnalysisSummaryVo vo = new AnalysisSummaryVo();

        //3.封装vo
        vo.setCumulativeUsers(analysisByDayMapper.findUserTotal());
        vo.setActivePassMonth(analysisByDayMapper.findActiveUserCountInPeriod(passMonthDay,yesterDay));
        vo.setActivePassWeek(analysisByDayMapper.findActiveUserCountInPeriod(passWeekDay,yesterDay));

        vo.setNewUsersToday(todayAnalysis.getNumRegistered().longValue());
        vo.setNewUsersTodayRate(ComputeUtil.computeRate(todayAnalysis.getNumRegistered(),yesterdayAnalysis.getNumRegistered()));

        vo.setLoginTimesToday(todayAnalysis.getNumLogin().longValue());
        vo.setLoginTimesTodayRate(ComputeUtil.computeRate(todayAnalysis.getNumLogin(), yesterdayAnalysis.getNumLogin()));

        vo.setActiveUsersToday(todayAnalysis.getNumActive().longValue());
        vo.setActiveUsersTodayRate(ComputeUtil.computeRate(todayAnalysis.getNumActive(),yesterdayAnalysis.getNumActive()));

        //4.返回vo
        return vo;
    }
}